const url = '127.0.0.1:3000'
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包分析
const CompressionWebpackPlugin = require('compression-webpack-plugin') // gzip
const productionGzipExtensions = ['js', 'css', 'html'] // 需要gzip压缩的文件后缀
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const path = require('path');
const glob = require('glob');
// const ImageminPlugin = require("imagemin-webpack");
const setManyPages = () => {
    const projectRoot = process.cwd();
    const entry = glob.sync(path.join(projectRoot, '/src/*/main.ts'))
    const template = glob.sync(path.join(projectRoot, '/public/*.html'));
    const pages = {};
    const dirs = [];
    template.map(item => {
        const new_t = item.match(/[a-zA-Z]+\.html$/g)[0].replace(/.html$/, '');
        pages[new_t] = {
            entry: '',
            template: '',
            filename: ''
        }
        pages[new_t].entry = entry.find(it => new RegExp(new_t, 'g').test(it))
        pages[new_t].template = item
        pages[new_t].filename = `${new_t}.html`;
        dirs.push(new_t)
    })
    return {
        pages,
        dirs
    }
}
const { pages, dirs } = setManyPages();
console.log(dirs)
function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = {
    publicPath: '/', // 公共路径
    outputDir: 'dist',
    pages,
    css: {
        loaderOptions: {
            less: {
                javascriptEnabled: true
                    // data: `@import "~@/styles/variables.less";`
            }
        },
        sourceMap: false // 是否为 CSS 开启 source map。设置为 true 之后可能会影响构建的性能
    },
    lintOnSave: true, // 开启eslint,在生产构建时禁用 eslint-loader
    chainWebpack: (config) => {
        // config.resolve.alias
        //     .set('assets', resolve('src/assets'))
        // config.module
        //     .rule('svg')
        //     .exclude.add(resolve('src/components/common/icons'))
        //     .end()
        // config.module
        //     .rule('icons')
        //     .test(/\.svg$/)
        //     .include.add(resolve('src/components/common/icons'))
        //     .end()
        //     .use('svg-sprite-loader')
        //     .loader('svg-sprite-loader')
        //     .options({
        //         symbolId: 'icon-[name]'
        //     })
        //     .end()

    },
    configureWebpack: config => {
        // 支持字符串模板编译
        config.resolve = {
            extensions: ['.js', '.ts', '.tsx', '.vue', '.json', '.css', '.less'],
            alias: {
                vue$: 'vue/dist/vue.esm.js',
                '@': resolve('src')
            },
            mainFields: ['main']
        };
        config.plugins.push(...[
            new HardSourceWebpackPlugin() // 开启缓存，进行二次构建优化
        ]);
        if (process.env.NODE_ENV === 'production') {
             // 打包分析
            config.plugins.push(
                new BundleAnalyzerPlugin({
                analyzerMode: "static"
                }),
                new CompressionWebpackPlugin({
                    algorithm: 'gzip',
                    test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                    threshold: 10240,
                    minRatio: 0.8
                })
            ) 
            // 关闭 webpack 的性能提示
            return {
                performance: {
                    hints: false
                }
            }
        }
    },
    parallel: require('os').cpus().length > 1,
    // pwa: {},
    devServer: {
        proxy: {
            '/api': {
                target: url,
                ws: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }
        },
        overlay: {
            warnings: true,
            errors: true
        }
    },
    productionSourceMap: false
}